package com.lms.controller;

import com.lms.bean.RentRecord;
import com.lms.bean.ReturnRecord;
import com.lms.bean.User;
import com.lms.service.ReturnRecordService;
import com.lms.util.LogUtils;
import com.lms.util.ResultUtils;
import com.lms.vo.RentRecordVO;
import com.lms.vo.ReturnRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("returnRecord")
public class ReturnRecordController {

    @Autowired
    private ReturnRecordService returnRecordService;


    @RequestMapping("admin/returnRecord-list")
    public String toReturnRecordList() {
        return "admin/returnRecord-list";
    }


    @RequestMapping("admin/returnRecord-input")
    public String toReturnRecordInput(Long returnId, Model model) {
        if(returnId == -1){//添加
            ReturnRecord returnRecord = new ReturnRecord();
            model.addAttribute("returnRecord",returnRecord);
        }else{//编辑
            ReturnRecord returnRecord = returnRecordService.findById(returnId);
            model.addAttribute("returnRecord",returnRecord);
        }
        return "admin/returnRecord-input";
    }


    @RequestMapping("admin/updateReturnRecord")
    public String updateReturnRecord(ReturnRecord returnRecord){
        LogUtils.printLog(returnRecord.getReturnId().toString());
        returnRecordService.save(returnRecord);
        return "admin/returnRecord-list";
    }


    @RequestMapping("admin/returnRecord-delete")
    public String deleteById(Long returnId){
        returnRecordService.deleteById(returnId);
        return "admin/returnRecord-list";
    }


    @RequestMapping("admin/deleteChecked")
    public String deleteChecked(String returnRecordList){
        String[] returnRecords = returnRecordList.split(",");
        List<Long> ids = new ArrayList<>();
        for (String s : returnRecords){
            ids.add(Long.valueOf(s));
        }
        returnRecordService.deleteAll(ids);
        return "admin/returnRecord-list";
    }


    @RequestMapping("all")
    @ResponseBody
    public ResultUtils<ReturnRecordVO> findAll(@RequestParam("page") Integer pageFromLayUI,
                                               @RequestParam("limit") Integer limitFromLayUI,
                                               @RequestParam(value ="searchByUserName",defaultValue = "")  String searchByUserName,
                                               @RequestParam(value ="searchByBookName",defaultValue = "")  String searchByBookName){
        String username = "%"+searchByUserName+"%";
        String bookname = "%"+searchByBookName+"%";
        //获取记录的总数，交给前端计算分页
        Long count = returnRecordService.getTotalCount(username,bookname);
        //模糊查询，如果是空则查询全部
        //获取当前页的记录列表交给前端渲染,注意 layui 和 pageable 关于 page 的计数差别
        List<ReturnRecordVO> data = returnRecordService.findAll(pageFromLayUI - 1, limitFromLayUI,username,bookname);
        //返回JSON结果
        return ResultUtils.layUITable(count, data);
    }

    /**
     * 返回该用户所有可以还书的订单
     */
    @RequestMapping("allReturns")
    @ResponseBody
    public ResultUtils<RentRecordVO> findAllReturns(@RequestParam("page") Integer pageFromLayUI,
                                                    @RequestParam("limit") Integer limitFromLayUI,
                                                    @RequestParam(value ="searchByBookName",defaultValue = "")  String searchByBookName,
                                                    HttpSession session){
        //模糊查询，如果是空则查询全部
        String bookname = "%"+searchByBookName+"%";
        User user = (User) session.getAttribute("user");
        //获取记录的总数，交给前端计算分页
        Long count = returnRecordService.getTotalReturnsCount(user.getUserId(),bookname);
        //获取当前页的记录列表交给前端渲染,注意 layui 和 pageable 关于 page 的计数差别
        List<RentRecordVO> data = returnRecordService.findAllReturns(pageFromLayUI - 1, limitFromLayUI, user, bookname);
        //返回JSON结果
        return ResultUtils.layUITable(count, data);
    }

    /**
     * 点击还书，添加还书记录
     * 修改图书的数量
     */
    @RequestMapping("user/addReturnRecord")
    public String addReturnRecord(ReturnRecord returnRecord){
        LogUtils.printLog(returnRecord.getReturnId().toString());
        returnRecordService.returnBook(returnRecord);
        return "user/returnRecord-list";
    }

}